package br.uniminas.persistencia;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.uniminas.entidades.Funcionario;

public class FuncionarioDao {

	private List<Funcionario> listaFuncionarios;
	private Funcionario funcionario;
	private Session session = HibernateUtil.getSessionFactory().getCurrentSession();

	public List todos() {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			listaFuncionarios = session.createQuery("from Funcionario as e left join fetch e.departamento d").list();
			return listaFuncionarios;
		} finally {
			session.close();
		}
	}

	public Funcionario getEmployee(Integer id) {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from Funcionario as e inner join fetch e.departamento d where e.id =:id");
			q.setInteger("id", id);
			return (Funcionario) q.uniqueResult();
		} finally {
			session.close();
		}
	}

	public void insert(Funcionario emp) {
		session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(emp);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	public void update(Funcionario emp) {
		session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(emp);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	public void delete(Integer id) {
		session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			funcionario = (Funcionario) session.get(Funcionario.class, id);
			session.delete(funcionario);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	public List buscaFuncionariosPorDepartamento(int id) {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from Funcionario as e where e.departamento.id =:id");
			q.setInteger("id", id);
			List l = q.list();
			return l;
		} finally {
			session.close();
		}
	}

	public List<Funcionario> buscaFuncionariosPorNome(String nome) {
		System.out.println(nome);
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from Funcionario as f where f.nome like :nome");
			q.setString("nome", "%"+nome+"%");
			List l = q.list();
			return l;
		} finally {
			session.close();
		}
	}

	public List<Funcionario> buscaFuncionariosPorIdade(int idade) {
		session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from Funcionario as f where f.idade = :idade");
			q.setInteger("idade", idade);
			List l = q.list();
			return l;
		} finally {
			session.close();
		}
	}
}
